package com.glorysoft.tonglingxjpda.ui.function.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseActivity
import com.glorysoft.tonglingxjpda.base.BaseViewModel
import com.glorysoft.tonglingxjpda.databinding.ActivityPackageBinding
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.ui.function.viewmodel.PackageViewModel
import com.pda.platform.ui.ui_pdaplatform.activity_public.FreeUI_QRCodeActivity
import kotlinx.android.synthetic.main.activity_package.*
import kotlinx.android.synthetic.main.activity_package.etBatchID
import kotlinx.android.synthetic.main.activity_track.*

/**
 * FileName: PackageActivity
 * Author: XJH
 * Date: 2022/5/5 17:08
 * Description:
 * History:
 */
class PackageActivity : BaseActivity<PackageViewModel,ActivityPackageBinding>(),
    View.OnClickListener {
    override fun layoutId(): Int {
        return R.layout.activity_package
    }

    override fun initVariableId(): Int {
        return BR.packageViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("包装界面")

        rvPac.setEmptyView(pacEmpty)
        rvFinPac.setEmptyView(finPacEmpty)
        rvBox.setEmptyView(boxEmpty)
        tvPackage.setOnClickListener(this)
        ivBox.setOnClickListener(this)
        ivBatchID.setOnClickListener(this)
    }

    override fun initData() {

        initEdit(etBox,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.initBoxData(s)
            }

        })

        initEdit(etBatchID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.initPacData(etBox.text.toString(),s)
            }

        })
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.ivBox  ->{
                startScanForCode(1001)
            }
            R.id.ivBatchID ->{
                startScanForCode(1002)
            }
            R.id.tvPackage ->{
                if (viewModel.PacData.isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到batchList信息",false)
                    return
                }

                viewModel.submit(etBox.text.toString(),etBatchID.text.toString())
            }
        }

    }

    private fun startScanForCode(code: Int) {
        val intent = Intent(this, FreeUI_QRCodeActivity::class.java)
        //设置横屏
        intent.putExtra("screenOrientation",1)
        startActivityForResult(intent,code)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK){
            val barCode = data!!.getStringExtra("barCode")
            when(requestCode){
                1001 -> {
                    etBox.setText(barCode)
                    viewModel.initBoxData(barCode)
                }
                1002 -> {
                    etBatchID.setText(barCode)
                    viewModel.initPacData(etBox.text.toString(),barCode)
                }
            }

        }
    }
}